<?php
class Manager_CategorySolutionController extends Zendvn_Controller_Action{
	private $_arrParam;
	private $_mainAction;
	protected $_currentController;
	protected $_paginator = array(
									'itemCountPerPage' => 5,
									'pageRange' => 3,
								  ); 
	
	public  function init()
	{
	    $this->_arrParam = $this->_request->getParams();	
        $this->loadTemplate(TEMPLATE_PATH . '/admin/system');
        
        $this->_mainAction = $this->_arrParam['module'] . '/' . $this->_arrParam['controller'] . '/index';	
	    $this->view->arrParam = $this->_arrParam; 

        $ns = new Zend_Session_Namespace('login'); 
	    if(($ns->id)<1) 
	      {
	      $this->_redirect('manager/');
	      }      
	      
	    $tbl_menu = new Manager_Model_menu();
	    $this->view->language=$tbl_menu->get_language();
	}
	
	public function indexAction()
	{
	 
	    $this->view->Title = "Category Solution Manager";
		$this->view->headTitle($this->view->Title,true);
		
		$tbl_category              = new Manager_Model_categorysolution();
		$this->view->result        = $tbl_category->listsec();
		
		if($this->_request->getCookie('status')==NULL){ $status=-1; } 
			 else { $status=$this->_request->getCookie('status');}
		if($this->_request->getCookie('secnews')==NULL){ $secnews=-1; } 
			else { $secnews=$this->_request->getCookie('secnews');}	 
			    
	  
	
		if($this->_request->getPost('keyword')!=NULL){
			 $keyword=$this->_request->getParam('keyword');
			 $this->view->keyword=$keyword;


     	 	 $listArticles = $tbl_category->searchkq($keyword,$status,$secnews);
          	 $currentPage = 1;
         	 $i = $this->_getParam('page',1);
         	 if(!empty($i))
         	 {
				 $currentPage = $i;
          	 }
         	 $paginator = Zend_Paginator::factory($listArticles);
         	 $paginator->setItemCountPerPage(50)
                    ->setPageRange(5)
                    ->setCurrentPageNumber($currentPage);
         	 $this->view->paginator = $paginator;
		 } 
    	else 
         {
     	 	$listArticles = $tbl_category->listItemabout($status,$secnews);
     	 	
     	 	$currentPage = 1;
         	 $i = $this->_getParam('page',1);
         	 if(!empty($i))
         	 {
          	    $currentPage = $i;
          	}
         	 $paginator = Zend_Paginator::factory($listArticles);
         	 $paginator->setItemCountPerPage(50)
                    ->setPageRange(10)
                    ->setCurrentPageNumber($currentPage);
         	 $this->view->paginator = $paginator;
         	
		}
   
	}
		
	public function statusAction(){
		
   $db = Zend_Registry::get('connectDb');
   $arrParam=$this->_arrParam;
   if($this->_arrParam['type'] == 'multi'){
		    
			if(count($arrParam['cid'])>0){
				$ids = implode(',', $arrParam['cid']);
				$status = ($arrParam['s'] == 0 )? 1:0;  
				$where = 'id IN (' . $ids . ')'; 
			    $db->update('category_solution',array('published'=>$status),$where);
			}
			 }
	else 
	     {
			$status = ($arrParam['s'] == 0 )? 1:0;            
			$where ="id=$arrParam[id]";  
			$db->update('category_solution',array('published'=>$status),$where);			
		   	 
	        }
	 $this->_redirect($this->_mainAction);
	 $this->_helper->viewRenderer->setNoRender();
	  }

	public function featuredAction(){
		
   $db = Zend_Registry::get('connectDb');
   $arrParam=$this->_arrParam;
   if($this->_arrParam['type'] == 'multi'){
		    
			if(count($arrParam['cid'])>0){
				$ids = implode(',', $arrParam['cid']);
				$status = ($arrParam['s'] == 0 )? 1:0;  
				$where = 'id IN (' . $ids . ')'; 
			    $db->update('category_solution',array('featured'=>$status),$where);
			}
			 }
	else 
	     {
			$status = ($arrParam['s'] == 0 )? 1:0;            
			$where ="id=$arrParam[id]";  
			$db->update('category_solution',array('featured'=>$status),$where);			
		   	 
	        }
	 header("location:".$_SERVER['HTTP_REFERER']);
	 $this->_helper->viewRenderer->setNoRender();
	  }
	
	public function sortAction(){ 
		$db = Zend_Registry::get('connectDb');
		if($this->_request->isPost()){
		  
		foreach ($this->_arrParam['cid'] as $val){ 
		$where = 'id = ' . $val;
		$db->update('category_solution',array('order'=>$this->_arrParam['order'][$val]),$where);
		
		}
		     $this->_redirect('manager/category-solution');
		     $this->_helper->viewRenderer->setNoRender();
		} 
      }
	
    public function addAction()
	{
	  $this->view->headScript()->appendFile($this->view->baseUrl().'/ckeditor/ckeditor.js');
	  $this->view->headScript()->appendFile($this->view->baseUrl().'/ckfinder/ckfinder.js');
	  
	  $this->view->Title = "Administrator ::Category Add Item.";
	  $this->view->headTitle($this->view->Title,true);
	  $tbl_category              = new Manager_Model_categorysolution();
	  $this->view->result        = $tbl_category->listsec();
	 if($this->_request->isPost()==true)
		{	
		   $data=$this  ->_arrParam;
		   
		   if ($data['title']=='')
		   {
		   	  $error=1;
		      $this->view->error  ='Enter your title category. ';
		   }
		   elseif ($data['section_id']=='0')
		     {
		      $error=1;
		      $this->view->error  ='Choose you section';
		     }
		   elseif ($data['language_id']=='0')
		     {
		      $error=1;
		      $this->view->error  ='Select language';
		     }
		     
		 if (empty($error)==true)
		    {
		     $tbl_category->save($data);	
	         $this->_redirect('manager/category-solution');	
		    }
		}
	}
	
	public function editAction()
	{
	  $this->view->headScript()->appendFile($this->view->baseUrl().'/ckeditor/ckeditor.js');
	  $this->view->headScript()->appendFile($this->view->baseUrl().'/ckfinder/ckfinder.js');
	  
	  $this->view->Title = "Administrator ::Category Add Item.";
	  $this->view->headTitle($this->view->Title,true);
	  
	  $tbl_category = new Manager_Model_categorysolution();
	  $this->view->abc= $tbl_category->listsec();
	  
	  
	  $id=$this->_request->getParam('id');
	  
	  $result=$tbl_category->getrow($id);
	  $this->view->result=$result;
	  $this->view->data=$result;
	  
	  if ($this->_request->isPost())  
		  {
		  	$data=$this->_arrParam;
			$tbl_category->save($data);	
			$this->_redirect('manager/category');
		  } 
    
	}

	public  function deleteAction()
	{
		$res = new Manager_Model_categorysolution();
		$id=$this->_request->getParam('id');
		$res->delete_data($id);
        $res->delete_data_content($id);
		$this->_redirect('manager/category-solution');
	}
	
    public function mutiAction()
	{
	    $tblsection=new Manager_Model_categorysolution();
		$tblsection->delete_muti($this->_arrParam);
		$this->_redirect('manager/category-solution');
		$this->_helper->viewRenderer->setNoRender();
	}
	
   
}